/*============ This creates Figure 3 + Appendix Figure 4: Stacked DD Estimates (b), (d), and (f) ===============*/

/* Note: This only works for state-by-year panel (not individual data)*/

cap program drop createstack
program define createstack
args prominent smallcut smallcont //prominent = size of increase smallcut = smaller mw to cut, smallcont = smaller mw to control

//Merge in federal mw 
preserve
use "${data2}mw_state_annual.dta" , clear
ren statefips state_fips
replace year = year+1
tempfile fedmw
save `fedmw'
restore

merge 1:1 state_fips year using `fedmw', keep(3) nogen keepusing(mean_fed_mw)
gen byte fed_mw = round(mean_fed_mw,.0001)==round(mw,.0001) 

sort state_fips year
xtset state_fips year

cap drop mw_change
gen mw_change = d1.mw
replace mw_change=0 if mw_change==.

gen fed_mw_change = 1 if mw_change>0 & fed_mw==1
replace fed_mw_change = fed_mw_change*year
replace mw_change=0 if fed_mw_change!=.

gen tryear_eff = year if mw_change>=`prominent'/100


bys state_fips: egen tryear_eff_smallest = min(tryear_eff)
replace tryear_eff=. if tryear_eff!=tryear_eff_smallest 
drop tryear_eff_smallest


sort tryear_eff state_fips 
gen eventid=_n if tryear_eff!=.

qui sum eventid 
local max = r(max)


/*========= TAKE 1: =========*/

local eventno = ""
qui{
foreach i of numlist 1/`max' {
	preserve
	
	** Figure out Treatment State + Year
	sum tryear if eventid==`i'
	global tryear = r(mean)
				
	sum state_fips if eventid==`i'
	global trfips = r(mean)
	
	
	***Sample Cuts
				
	//Keep donor states w/o "treatment" 
	local lead = $lead
	local lag = $lag
	keep if inrange(year, $tryear -`lead', $tryear + `lag') 
	drop if mw_change >=`smallcut' & state_fips!=$trfips
				
	//Balanced Panel -- Treatment State should be balanced. 
	bys state_fips: gen count=_N
	qui sum count if state_fips==$trfips
	local numobs= r(mean)
	
	keep if count==`numobs'
	
	//Create smaller mw control & fed mw
	gen small_inc = year if mw_change>=`smallcont'/100
	bys state_fips: egen small_year = min(small_inc) 
	bys state_fips: egen fed_year = min(fed_mw_change) 
	gen byte mw_small = year >= small_year & state_fips!=$trfips
	gen byte mw_fed = year >= fed_year & state_fips!=$trfips
		
	//Keep only cases where there are control groups
	sum state_fips if state_fips!=$trfips
	local N =r(N) 
	if `N' >0  {
		sort state_fips year
		replace mw_change=0
		replace mw_change=1 if state_fips==$trfips & tryear==$tryear
		forval f = $lead(-1)1 {
			gen F`f'_D = F`f'.mw_change
			label var  F`f'_D "-`f'"
			replace F`f'_D = 0 if F`f'==.
			}
		forval l = 0/$lag  {
			gen L`l'_D = L`l'.mw_change
			label var  L`l'_D "`l'"
			replace L`l'_D = 0 if L`l'==.
		}
		
		gen event=`i'
		replace tryear=. if  state_fips!=$trfips
		tempfile event`i'
		save `event`i'', replace
		local eventno = "`eventno'" + "`i' " 
	}
	else {
	
	}
	restore
}

clear
foreach i of local eventno {
	append using `event`i''
}
}


replace year = year -1
merge m:1 state_fips year using "${data2}/aca_expansion_eitc.dta", keep(1 3) nogen
replace year = year + 1
end


cap program drop plotes
program define plotes

  local range yla(-0.04(0.01)0.04)
  		coefplot ///
  			(D, omitted keep(F*D L*D )  msize(small) recast(connected) lcolor(gs1) color(gs1) lwidth(thin) ciopts(recast(rcap)  lcolor(gs10) lwidth(thin))),  ///
  			vertical xline(3.5) yline(0) graphregion(color(white)) yla(, nogrid) ///
  		   `range' legend(off) nooffsets ///
  			ytitle("Estimated Effect of Prominent Minimum Wage") xtitle("Years Since Prominent Minimum Wage Increase")


end

global lead=3
global lag=3

local rhs ur_high_edu wage_high_edu hpi eitc tanf4 snap4 ACA mw_small mw_fed
local rhs2 ur_high_edu wage_high_edu hpi eitc tanf4 snap4 ACA


/// 2011-2020
foreach inc of numlist 25 50 75 100 {

	use ${data1}state_panel_mw_poverty, clear
	keep if dem==1

	if `inc'>50 {
		createstack `inc' 50 10
	} 
	else if `inc'==50  {
	createstack `inc' 25 10
	}
	
	else {
	createstack `inc' 10 5
	}

	replace F1_D=0
	reghdfe under_100 F* L* `rhs' [pw=asecwt], absorb(state_fips event year) cl(state_fips)
	est save ${est}ster/prominent_stacked_es_`inc', replace
}

/// 2011-2020 -- Exclude all
foreach inc of numlist 25 50 75 100 {

	use ${data1}state_panel_mw_poverty, clear
	keep if dem==1

	if `inc'>50 {
		createstack `inc' 0.0001 0.0001
	} 
	else  {
	createstack `inc' 0.0001 0.0001
	}

	replace F1_D=0
	reghdfe under_100 F* L* `rhs' [pw=asecwt], absorb(state_fips event year) cl(state_fips)
	est save ${est}ster/prominent_stacked_es_`inc'_noinc, replace
}


// Dube Period
foreach inc of numlist 50 75 100 {
	use ${data1}state_panel_mw_poverty_84, clear
	keep if year<2014

	if `inc'>50 {
		createstack `inc' 50 10
	} 
	else if `inc'==50  {
	createstack `inc' 25 10
	}
	
	else {
	createstack `inc' 10 5
	}
	
	replace F1_D=0
	reghdfe under_100 F* L* `rhs' [pw=asecwt], absorb(state_fips event year) cl(state_fips)
	est save ${est}ster/prominent_stacked_es_`inc'_8413, replace
}

//1984-2020
foreach inc of numlist 25 50 75 100 {
	use ${data1}state_panel_mw_poverty_84, clear

	if `inc'>50 {
		createstack `inc' 50 10
	} 
	else if `inc'==50  {
	createstack `inc' 25 10
	}
	
	else {
	createstack `inc' 10 5
	}
	
	replace F1_D=0
	reghdfe under_100 F* L* `rhs' [pw=asecwt], absorb(state_fips event year) cl(state_fips)
	est save ${est}ster/prominent_stacked_es_`inc'_84, replace
}


// Plot
foreach inc of numlist 25 50 75 100 {
  estimates use ${est}ster/prominent_stacked_es_`inc'
  est store D
  plotes
  graph export "${fig}event_studies/stacked_es_`inc'_st.png", replace
  
  estimates use ${est}ster/prominent_stacked_es_`inc'_noinc
  est store D
  plotes
  graph export "${fig}event_studies/stacked_es_`inc'_noinc_st.png", replace
   
  estimates use ${est}ster/prominent_stacked_es_`inc'_8413
  est store D
  plotes
  graph export "${fig}event_studies/stacked_es_8413_`inc'_st.png", replace
  
   estimates use ${est}ster/prominent_stacked_es_`inc'_84
  est store D
  plotes
  graph export "${fig}event_studies/stacked_es_84_`inc'_st.png", replace
}
